package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.example.demo.controller.orderSystem.OrderManage;
import com.example.demo.entity.*;
import com.example.demo.mapper.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/orderapi")
public class OrderControllor {

    @Resource
    OrderMapper orderMapper;
    @Resource
    UserMapper userMapper;

    @Resource
    OrderBasicInfoMapper orderBasicInfoMapper;

    @Resource
    AcceptInfoMapper acceptInfoMapper;

    @Resource
    SendInfoMapper sendInfoMapper;

    @Resource
    UserCommonInfoMapper userCommonInfoMapper;

    @Data
    public static class CreateOrder{
        String sender_phone;
        String sender_address;
        String accept_phone;
        String accept_address;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        Date sender_time;
    }
    @Data
    public static class NeedCreateOrder{
        CreateOrder newOrderMap;
    }
    @Data
    static class Phone{
        String phone;
    }
    @Data
    static class IdPhone{
        String phone;
        String user_id;
    }

    @GetMapping("/test")
    public String test(){

        System.out.println("test");
        return "test";
    }


    @GetMapping( "/find_all_accept_info")
    public ReturnStatus<List<AcceptInfo> > find_all_accept_info(){
        System.out.println("23123");
        ReturnStatus<List<AcceptInfo> > my = new ReturnStatus<List<AcceptInfo> >(1,acceptInfoMapper.find_all_accept_info());
        return my;
    }
    @PostMapping( "/find_order_status")
    public int find_order_status(@RequestBody OrderBasicInfo orderBasicInfo){
        System.out.println(orderBasicInfo.getOrder_id());
        return orderBasicInfoMapper.find_order_status(orderBasicInfo);
    }

    @GetMapping( "/find_all_send_info")
    public ReturnStatus<List<SendInfo> >find_all_send_info(){

        System.out.println(acceptInfoMapper.find_all_send_info());
        return new ReturnStatus<List<SendInfo> >(1,acceptInfoMapper.find_all_send_info());
    }
    @PostMapping("/find_my_accept_info")
    public List<AcceptInfo> find_my_accept_info(@RequestBody Phone myphone){
        return orderMapper.find_my_accept_info(myphone.phone);
    }

    @PostMapping("/find_my_send_info")
    public List<SendInfo> find_my_send_info(@RequestBody Phone myphone){
        return orderMapper.find_my_send_info(myphone.phone);
    }
    @PostMapping("/find_my_user_common_info")
    public List<UserCommonInfo> find_my_user_common_info(@RequestBody Phone myphone){
        return userCommonInfoMapper.find_my_user_common_info(myphone.phone);
    }



    @PostMapping("/delete_my_user_common_info")
    public int delete_my_user_common_info(@RequestBody IdPhone idPhone){
        return userCommonInfoMapper.delete_my_user_common_info(idPhone.user_id,idPhone.phone);
    }

    @PostMapping("/update_my_user_common_info")
    public int update_my_user_common_info(@RequestBody UserCommonInfo userCommonInfo){
        return userCommonInfoMapper.update_my_user_common_info(userCommonInfo);
    }


    @PostMapping("/insert_my_user_common_info")
    public int insert_my_user_common_info(@RequestBody UserCommonInfo userCommonInfo){
//        userCommonInfo.setUser_common_name(UTF2GBK.utf82gbk(userCommonInfo.getUser_common_name()));
//        userCommonInfo.setUser_common_address(UTF2GBK.utf82gbk(userCommonInfo.getUser_common_address()));
//
//        System.out.println(userCommonInfo.getUser_common_address());
//        System.out.println(userCommonInfo.getUser_common_name());
        return userCommonInfoMapper.insert_my_user_common_info(userCommonInfo);
    }

    @PostMapping("/get_user_id")
    public String get_user_id(@RequestBody Phone myphone){
        System.out.println(myphone.phone);
        return userMapper.get_user_id(myphone.phone);
    }

    @PostMapping("/create_order")
    public ReturnStatus<String> create_order(@RequestBody NeedCreateOrder needCreateOrder){
        CreateOrder createOrder  = needCreateOrder.newOrderMap;
        String order_id = null;
        System.out.println(createOrder.accept_phone);
        System.out.println(createOrder.sender_phone);
        //创建订单，
        order_id = OrderManage.createNewOrder(createOrder);

        //创建失败
        if(order_id == null){
            System.out.println("this");
            return new ReturnStatus<String> (0,null);
//            return Controller.returnStatus(0,(JSON)JSON.toJSON(order_id));
        }
        return new ReturnStatus<String> (1,order_id);
//        return Controller.returnStatus(1,(JSON)JSON.toJSON(order_id));
    }


    @PostMapping("/cancel_order")
    public ReturnStatus<String> cancelOrder(@RequestBody OrderBasicInfo orderBasicInfo){
        System.out.println(orderBasicInfo.getOrder_id());
        System.out.println(orderBasicInfo.getOrder_status());

        //取消失败
        if(OrderManage.cancelNewOrder(orderBasicInfo) == false)
            return new ReturnStatus<String> (0,null);

        //取消成功
        return new ReturnStatus<String> (1,null);
    }


    @PostMapping("/accept_order")
    public ReturnStatus<String> acceptOrder(@RequestBody AcceptInfo acceptInfo){
        acceptInfoMapper.updateByPrimaryKey(acceptInfo);
        return new ReturnStatus<String> (1,null);
    }

    @PostMapping("/send_order")
    public ReturnStatus<String> sendOrder(@RequestBody SendInfo sendInfo){
        sendInfoMapper.updateByPrimaryKey(sendInfo);
        return new ReturnStatus<String> (1,null);
    }



    @PostMapping("/pay_order")
    public ReturnStatus<String> payOrder(@RequestBody OrderBasicInfo orderBasicInfo){

        //返回状态为0表示失败，因为订单不存在或状态不对
        if(orderBasicInfo.getOrder_status() != 0)
            return new ReturnStatus<String> (0,null);

        //更改状态为1
        orderBasicInfo.setOrder_status(1);
        orderBasicInfoMapper.updateByPrimaryKey(orderBasicInfo);
        return new ReturnStatus<String> (1,null);
    }
}






